home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d1 / go2.arc / LISTING2.C < prev    next >
Encoding:
Text File  |  1989-07-21  |  4.0 KB  |  175 lines

  1. /* This version is for Turbo C 2.0
  2.  *
  3.  * Compilation syntax:
  4.  *   tcc -w -G go.c
  5.  */
  6.  
  7.  
  8.  
  9.  
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <dos.h>
  13. #include <dir.h>
  14. #include <string.h>
  15.  
  16. #ifndef TRUE
  17.     #define TRUE  1
  18.     #define FALSE 0
  19. #endif
  20.  
  21. extern unsigned _stklen = 0x1000;
  22.  
  23. int   file_search = TRUE;
  24. int   dir_search  = TRUE;
  25. int   orig_drive;
  26. char *orig_dir;
  27. int   search_drive;
  28. char *search_string;
  29. char *dir_search_string;
  30. int   dir_search_length;
  31. char *file_search_string;
  32. int   reset_drive = FALSE;
  33. int   reset_dir   = FALSE;
  34.  
  35. void do_resets(void);
  36. void help(void);
  37. void test_dir(void);
  38.  
  39. void main(int argc, char **argv)
  40. {
  41.     unsigned new_drive;
  42.  
  43.     atexit(do_resets);
  44.  
  45.     if (argc < 2 || argc > 3)
  46.         help();
  47.  
  48.     orig_drive = getdisk();
  49.     orig_dir = getcwd(NULL,MAXDIR);
  50.  
  51.     if (argc == 3)
  52.         if (strcmpi(argv[2],"-F") == 0 || strcmpi(argv[2],"/F") == 0)
  53.             dir_search  = FALSE;
  54.         else
  55.             help();
  56.  
  57.     search_string = strdup(strupr(argv[1]));
  58.     if(search_string[1] == ':')
  59.         {
  60.         new_drive = search_string[0] - 'A';
  61.         setdisk(new_drive);
  62.         reset_drive = TRUE;
  63.         search_string += 2;
  64.         }
  65.  
  66.     if (search_string[0] == '\\')
  67.         {
  68.         search_string++;
  69.         file_search = FALSE;
  70.         }
  71.  
  72.     if(strchr(search_string,'?') || strchr(search_string,'*'))
  73.         dir_search = FALSE;
  74.  
  75.     if (dir_search)
  76.         {
  77.         dir_search_string = search_string;
  78.         dir_search_length = strlen(dir_search_string);
  79.         }
  80.  
  81.     if (file_search)
  82.         {
  83.         file_search_string = malloc(strlen(search_string)+3);
  84.         strcpy(file_search_string, search_string);
  85.         if(! strchr(file_search_string, '.' ))
  86.             strcat(file_search_string, ".*");
  87.         }
  88.  
  89.     if (dir_search | file_search)
  90.         {
  91.         chdir("\\");
  92.         test_dir();
  93.         reset_dir = TRUE;
  94.         exit(0);
  95.         }
  96.     else
  97.         help();
  98. }
  99.  
  100. void help(void)
  101. {
  102.     puts("\nGO moves you quickly from one subdirectory to another.");
  103.     puts("Syntax:");
  104.     puts("      GO [d:][\\]pathname [-F]");
  105.     puts("         the pathname can be either the name of a directory or");
  106.     puts("         the name of a file.  It may contain wild cards.\n");
  107.     puts("         If 'd:' is included, drive 'd:' will be used instead");
  108.     puts("         of the current drive.\n");
  109.     puts("         If '\\' is included at the beginning of the pathname,");
  110.     puts("         only subdirectory names will be searched.\n");
  111.     puts("         If '-F' or /F' is included, or if the pathname includes");
  112.     puts("         wild card sybmols, only file names will be searched\n");
  113.     puts("         Normally, both file names and subdirectory names are");
  114.     puts("         searched to match the specified pathname.");
  115.     exit(0);
  116. }
  117.  
  118. void do_resets(void)
  119. {
  120.  
  121.     
  122.     if(reset_dir)
  123.         {
  124.         chdir(orig_dir);
  125.         puts ("   Requested subdirectory not found\a");
  126.         }
  127.     else
  128.         {
  129.         fputs("   New Directory: ",stdout);
  130.         puts (getcwd(NULL,MAXDIR));
  131.         }
  132.  
  133.     if(reset_drive)
  134.         setdisk(orig_drive);
  135. }
  136.  
  137. void test_dir(void)
  138. {
  139.     char  *current_dir, *cptr;
  140.     int    i;
  141.     struct ffblk find_buffer;
  142.  
  143.     current_dir = getcwd(NULL,MAXDIR);
  144.     if(dir_search && ((i = strlen(current_dir)) >= dir_search_length))
  145.         {
  146.         cptr = current_dir + (i - dir_search_length);
  147.         if (!strcmp(cptr,search_string))
  148.             exit(0);
  149.         }
  150.  
  151.     if(file_search)
  152.         {
  153.         if(!findfirst(file_search_string, &find_buffer, 0))
  154.             exit(0);
  155.         }
  156.  
  157.     findfirst("*.*", &find_buffer, FA_DIREC);
  158.     if(find_buffer.ff_attrib == FA_DIREC && find_buffer.ff_name[0] != '.')
  159.         {
  160.         chdir(find_buffer.ff_name);
  161.         test_dir();
  162.         chdir(current_dir);
  163.         }
  164.  
  165.     while (! findnext(&find_buffer))
  166.         if(find_buffer.ff_attrib == FA_DIREC && find_buffer.ff_name[0] != '.')
  167.             {
  168.             chdir(find_buffer.ff_name);
  169.             test_dir();
  170.             chdir(current_dir);
  171.             }
  172.  
  173.     free(current_dir);
  174. }